package com.project8.book;

import java.io.*;

import android.os.Environment;

public class FileRead 
{	
    public static String replaceOld(final String aInput, final String aOldPattern, final String aNewPattern)
    {
        if (aOldPattern.equals("")) 
        {
        	throw new IllegalArgumentException("Old pattern must have content.");
        }
        
        final StringBuffer result = new StringBuffer();
        int startIdx = 0;
        int idxOld = 0;
        while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) 
        {
        	result.append( aInput.substring(startIdx, idxOld) );
        	result.append( aNewPattern );
        	startIdx = idxOld + aOldPattern.length();
        }
        result.append( aInput.substring(startIdx) );
        return result.toString();
    }
    
    public static void nightDay(boolean Night, String BookName)
    {
    	String root = Environment.getExternalStorageDirectory().getAbsolutePath();
    	String path = root+"/Textbooks/" + BookName + "/res/";
    	
    	System.out.println(path);
        String aOldPattern = "";
        String aNewPattern = "";
        String aNewContent = "";
        
        try
        {
        	FileInputStream fstream = new FileInputStream(path + "Styles.css");
        	DataInputStream in = new DataInputStream(fstream);
        	BufferedReader br = new BufferedReader(new InputStreamReader(in));
        	String strLine;
        	
        	while ((strLine = br.readLine()) != null)
        	{
 
		        if(Night == true)
		        {
			        aOldPattern = "background-color:#DEDEDE"; 
			        aNewPattern = "background-color:#4A4A4A";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);
			        
			        aOldPattern = " solid #000000";
			        aNewPattern = " solid #FFFFFF";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);     
			        
			        aOldPattern = "background-color:#FFFFFF";
			        aNewPattern = "background-color:#000000";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);
			        
			        aOldPattern = " color:#000000";
			        aNewPattern = " color:#FFFFFF";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);
		        }
		        else
		        {
			        aOldPattern = "background-color:#4A4A4A";
			        aNewPattern = "background-color:#DEDEDE";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);
			        
			        
			        aOldPattern = "solid #FFFFFF";
			        aNewPattern = "solid #000000";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);
			        
			        aOldPattern = "background-color:#000000";
			        aNewPattern = "background-color:#FFFFFF";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);
			        
			        aOldPattern = " color:#FFFFFF";
			        aNewPattern = " color:#000000";
			        
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);
		        
		        }
		        aNewContent = aNewContent + strLine;
	        }
	        in.close();
        }
        catch (Exception e)
        {
        	System.err.println("Error: " + e.getMessage());
        }
        
        try
        {
	        FileWriter fstream = new FileWriter(path + "Styles.css");
	        BufferedWriter out = new BufferedWriter(fstream);
	        out.write(aNewContent);
	        out.close();
        }
        catch (Exception e)
        {
	        System.err.println("Error: " + e.getMessage());
        }
    }
    
    public static void changeSize(boolean up, String BookName)
    {
    	String root = Environment.getExternalStorageDirectory().getAbsolutePath();
    	String path = root + "/Textbooks/" + BookName + "/res/";
    	
        int curPos = 0;
        int prePos = 0;
        int numStart = 0;
        int oldSize = 0;
        boolean check = false;
        String aOldPattern = "";
        String aNewPattern = "";
        String aNewContent = "";
        
        try
        {
	        FileInputStream fstream = new FileInputStream(path +"Styles.css");
	        DataInputStream in = new DataInputStream(fstream);
	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
	        String strLine;
	        while ((strLine = br.readLine()) != null)
	        {
		        curPos = strLine.indexOf("font-size:", prePos);
		        while(curPos != -1)
		        {
			        curPos += 10;
			        numStart = curPos + 1;
			        while(check == false)
			        {
				        try
				        {
				        	oldSize = Integer.parseInt(strLine.substring(curPos, numStart));
				        	System.out.println("OLD FONT SIZE = " + oldSize);
				        }
				        catch(NumberFormatException ex)
				        {
				        	check = true;
				        }
				        
				        if(check == false)
				        	numStart++;
			        }
			        if(up == true)
			        {
				        aOldPattern = "font-size:" + Integer.toString(oldSize);
				        aNewPattern = "font-size:" + Integer.toString(oldSize + 1);
			        }
			        else
			        {
				        aOldPattern = "font-size:" + Integer.toString(oldSize);
				        aNewPattern = "font-size:" + Integer.toString(oldSize - 1);
			        }
			        System.out.println("NEW FONT SIZE SHOULD = " + oldSize);
			        strLine = replaceOld(strLine, aOldPattern, aNewPattern);				
			        prePos = curPos - 9;
			        curPos = strLine.indexOf("font-size:", prePos);
			        check = false;
		        }
		        aNewContent = aNewContent + strLine;
		        prePos = 0;
	        }
	        in.close();
        }
        catch (Exception e)
        {
        	System.err.println("Error: " + e.getMessage());
        }
        
        try
        {
	        FileWriter fstream = new FileWriter(path + "Styles.css");
	        BufferedWriter out = new BufferedWriter(fstream);
	        out.write(aNewContent);
	        out.close();
        }		
        catch (Exception e)
        {
        	System.err.println("Error: " + e.getMessage());
        }
    }
}
